//https://leetcode.cn/problems/squares-of-a-sorted-array/
package codeRandomThoughts.Test977有序数组的平方;


public class Solution {
    public int[] sortedSquares(int[] nums) {
        int[] res = new int[nums.length];
        int i = 0;
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int powLeft = pow(nums[left]);
            int powRight = pow(nums[right]);
            if (powLeft > powRight) {
                res[i] = powLeft;
                i++;
                left++;
            } else {
                res[i] = powRight;
                i++;
                right--;
            }
        }
        for (int j = 0; j < res.length; j++) {
            nums[j]=res[res.length-j-1];
        }
        return nums;
    }

    int pow(int a) {
        return a * a;
    }
}
